package com.xgry.bpm.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;

@Data
public class Result {

    //返回信息
    private String msg;
    //返回标示
    private int code;
    //返回数据
    private Object data;
    //每页条数
    private long pageSize;
    //当前页数
    private long pageIndex;
    //总条数
    private long total;

    public static Result success(){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        return result;
    }

    public static Result fail(){
        Result result = new Result();
        result.setCode(HttpStatus.METHOD_FAILURE.value());
        result.setMsg(HttpStatus.METHOD_FAILURE.getReasonPhrase());
        return result;
    }

    public static Result success(String msg){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(StringUtils.isNotEmpty(msg)?msg:HttpStatus.OK.getReasonPhrase());
        return result;
    }

    public static Result success(String msg , Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(StringUtils.isNotEmpty(msg)?msg:HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result success(Integer code , Object returnObj){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result success(Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result success(Object returnObj,long total){
        Result result = new Result();
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setData(returnObj);
        result.setTotal(total);
        return result;
    }

    public static Result fail(String msg){
        Result result = new Result();
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMsg(StringUtils.isNotEmpty(msg)?msg:HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        return result;
    }

    public static Result fail(int code , String msg , Object returnObj){
        Result result = new Result();
        result.setCode(code);
        result.setMsg(StringUtils.isNotEmpty(msg)?msg:HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result fail(String msg , Object returnObj){
        Result result = new Result();
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMsg(StringUtils.isNotEmpty(msg)?msg:HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        result.setData(returnObj);
        return result;
    }

    public static Result convertIpageToSuccess(IPage ipage){
        Result result = new Result();
        result.setData(ipage.getRecords());
        result.setCode(HttpStatus.OK.value());
        result.setMsg(HttpStatus.OK.getReasonPhrase());
        result.setPageIndex(ipage.getCurrent());
        result.setPageSize(ipage.getSize());
        result.setTotal(ipage.getTotal());
        return result;
    }

    public static void main(String[] args) {
        System.out.println();
    }

}